package com.whsw.scheduler.trading_center_scheduler.scheduler.reportFormScheduler;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 报市建局2定时器
 * @Author liangyiqiang 2021/03/19
 * */
@Component
public class HouseReport2Scheduler {
    @Autowired
    DSFactory dsFactory;

    /**
     * 报市住建局2-勘察设计
     * @Author liangyiqiang 2021/03/19
     * 还差额外字段：
     * construction_area建筑面积
     * project_type项目类型(CP表示房建项目,MP表示市政项目）
     * project_classify项目分类（D表示设计施工总承包项目，ND表示不含设计施工总承包项目）
     * bid_type招标类型（public表示公开招标，invited表示邀请招标）
     * */
    @Scheduled(cron = "0/10 * * * * ?")
    public void getSurveyDesignInfo() throws Exception {
        Db db1 = Db.use(dsFactory.getDataSource("a"));
        Db db2 = Db.use(dsFactory.getDataSource("b"));//我们数据库字段是double类型，对方的数据是decimal类型
        List<Entity> list = db2.query("" +
                "select a.package_code AS project_id," +
                "a.package_name AS project_name," +
                "sum(d.price) AS bid_price," +
                "c.publish_time AS create_time," +
                "c.create_by AS creater\n" +
                "from pub_t_package a\n" +
                "left join pub_t_project b on a.project_id = b.project_id \n" +
                "left join pub_t_result_letter c on c.package_id = a.package_id \n" +
                "left join gb_t_result_letter_enterprise d on d.result_letter_id = c.result_letter_id \n" +
                "where \n" +
                "c.approval_status = 'passed'\n" +
                "and c.status = 1\n" +
                "and b.tender_category_detail = 'FW_KCSJ'\n" +
                "and b.industry in('FJ','SZ')\n" +
                "group by a.package_code ");

        if(!ObjectUtils.isEmpty(list)){
            db1.del(
                    Entity.create("survey_design")
            );
            for (Entity entity : list) {
                entity.setTableName("survey_design");
            }
            db1.insert(list);
        }
    }
}
